//
//  SSBrowerController.swift
//  weibo
//
//  Created by 邵帅 on 16/7/7.
//  Copyright © 2016年 groupNineObject. All rights reserved.
//

import UIKit
import SnapKit

let SSPhotoBrowerViewControllerReuseIdentifier = "SSPhotoBrowerViewControllerReuseIdentifier"

class SSBrowerController: UIViewController {
    
    var pic_urls: [SSPhotoViewModel]?
    var indexPath: NSIndexPath?
    
    
    init(pic_urls: [SSPhotoViewModel], indexPath: NSIndexPath){
        
        self.pic_urls = pic_urls
        self.indexPath = indexPath
        
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        setupUI()
        
        setupCollectionView()
        
    }
    
    
    func setupUI() {
        
        view.addSubview(collectionView)
        view.addSubview(saveBtn)
        view.addSubview(closeBtn)
        
        collectionView.snp_makeConstraints { (make) in
            
            make.edges.equalTo(view.snp_edges)
        }
        
        saveBtn.snp_makeConstraints { (make) in
            
            make.width.equalTo(100)
            make.height.equalTo(30)
            make.left.equalTo(view.snp_left).offset(30)
            make.bottom.equalTo(view.snp_bottom).offset(-50)
        }
        
        closeBtn.snp_makeConstraints { (make) in
            
            make.width.equalTo(100)
            make.height.equalTo(30)
            make.right.equalTo(view.snp_right).offset(-30)
            make.bottom.equalTo(saveBtn)
        }
        
    }
    
    func setupCollectionView() {
        
        collectionView.dataSource = self
        collectionView.delegate = self
        
        // 注册
        collectionView.registerClass(SSBrowerCell.self, forCellWithReuseIdentifier: SSPhotoBrowerViewControllerReuseIdentifier)
        
        collectionView.pagingEnabled = true
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
    
    }
    
    
    
    
    
    
    // 懒加载
    private lazy var flowLayout: UICollectionViewFlowLayout = {
        
        let flowLayout = UICollectionViewFlowLayout()
        
        flowLayout.itemSize = self.view.bounds.size
        flowLayout.minimumLineSpacing = 0
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.scrollDirection = .Horizontal
        
        return flowLayout
    }()
    
    private lazy var collectionView: UICollectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: self.flowLayout)
    
    
    private lazy var saveBtn: UIButton = {
    
        let btn = UIButton(type: UIButtonType.Custom)
        
        btn.setTitle("保存", forState: .Normal)
        btn.backgroundColor = UIColor.darkGrayColor()
        btn.titleLabel?.font = UIFont.systemFontOfSize(16)
        
        btn.addTarget(self, action: #selector(SSBrowerController.saveBtnClick), forControlEvents: .TouchUpInside)
        
        return btn
    }()
    
    private lazy var closeBtn: UIButton = {
        
        let btn = UIButton(type: UIButtonType.Custom)
        
        btn.setTitle("关闭", forState: .Normal)
        btn.backgroundColor = UIColor.darkGrayColor()
        btn.titleLabel?.font = UIFont.systemFontOfSize(16)
        
        btn.addTarget(self, action: #selector(SSBrowerController.closeBtnClick), forControlEvents: .TouchUpInside)
        
        return btn
    }()
    
    
    // 保存按钮点击
    @objc private func saveBtnClick(){
        
        let visibleCell = collectionView.visibleCells()[0] as? SSBrowerCell
        
        visibleCell?.savePicture()
        
    }
    
    // 关闭点击
    @objc private func closeBtnClick(){
        
        dismissViewControllerAnimated(true, completion: nil)
        
    }
}



// 数据源方法
extension SSBrowerController: UICollectionViewDataSource,UICollectionViewDelegate {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return pic_urls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(SSPhotoBrowerViewControllerReuseIdentifier, forIndexPath: indexPath)as! SSBrowerCell
        
        cell.photoModel = pic_urls![indexPath.item]
        
        cell.closure = {
            
            self.closeBtnClick()
        }
        
        return cell
    }
    
    
  
}
